/**
 * Created on 2007-03-09 00:28
 */
package game.input;

import game.Block;
import game.media.MediaManager;
import game.media.audio.Sound;

/**
 * @author David Wallin
 * 
 * A basic hand control with two buttons, left and right
 * Register an instance of this class to one of the positions in the
 * InputManager, and then register a Block with the controller to get
 * a working environment
 */
public class Control
{
	private MediaManager		m_MediaManager = MediaManager.INSTANCE;
	
	private Block				m_Block;
	
	private final int			N_TIMES = 3;
	
	private boolean				m_LeftHasBeenPressedAtLeast_N_Times;
	private boolean				m_RightHasBeenPressedAtLeast_N_Times;
	private boolean				m_RotateHasBeenPressedAtLeast_N_Times;
	private boolean				m_DropHasBeenPressedAtLeast_N_Times;
	
	private int					m_LeftCounter;
	private int					m_RightCounter;
	private int					m_RotateCounter;
	private int					m_DropCounter;
	
	private Sound				m_MoveSound;
	private Sound				m_RotateSound;
	private Sound				m_DropSound;
	
	public Control()
	{
		m_Block = null;
		
		m_LeftHasBeenPressedAtLeast_N_Times = false;
		m_RightHasBeenPressedAtLeast_N_Times = false;
		m_RotateHasBeenPressedAtLeast_N_Times = false;
		m_DropHasBeenPressedAtLeast_N_Times = false;
		
		m_LeftCounter = 0;
		m_RightCounter = 0;
		m_RotateCounter = 0;
		m_DropCounter = 0;
		
		m_MoveSound = m_MediaManager.getAudioClip(MediaManager.SOUND_MOVE);
		m_RotateSound = m_MediaManager.getAudioClip(MediaManager.SOUND_ROTATE);
		m_DropSound = m_MediaManager.getAudioClip(MediaManager.SOUND_DROP);
	}
	
	public void setBlock(Block block)
	{
		m_Block = block;
	}
	
	public Block getBlock()
	{
		return m_Block;
	}
	
	public void pressLeft()
	{
		if(m_Block != null)
		{
			m_MoveSound.play(false);
			m_MoveSound.play(true);
			m_Block.setPosition(m_Block.getMatrisXpos() - 1, m_Block.getMatrisYpos());
			
			if(!m_LeftHasBeenPressedAtLeast_N_Times)
			{
				m_LeftCounter++;
				m_LeftHasBeenPressedAtLeast_N_Times = (m_LeftCounter > N_TIMES);
			}
		}
	}
	
	public void pressRight()
	{
		if(m_Block != null)
		{
			m_MoveSound.play(false);
			m_MoveSound.play(true);
			m_Block.setPosition(m_Block.getMatrisXpos() + 1, m_Block.getMatrisYpos());
			
			if(!m_RightHasBeenPressedAtLeast_N_Times)
			{
				m_RightCounter++;
				m_RightHasBeenPressedAtLeast_N_Times = (m_RightCounter > N_TIMES);
			}
		}
	}
	
	public void pressRotate()
	{
		if(m_Block != null)
		{
			m_RotateSound.play(false);
			m_RotateSound.play(true);
			m_Block.rotate();
			
			if(!m_RotateHasBeenPressedAtLeast_N_Times)
			{
				m_RotateCounter++;
				m_RotateHasBeenPressedAtLeast_N_Times = (m_RotateCounter > N_TIMES);
			}
		}
	}
	
	public void pressDropDown()
	{
		if(m_Block != null)
		{
			m_DropSound.play(false);
			m_DropSound.play(true);
			m_Block.setPosition(m_Block.getMatrisXpos(), m_Block.getMatrisYpos() + 1);
			
			if(!m_DropHasBeenPressedAtLeast_N_Times)
			{
				m_DropCounter++;
				m_DropHasBeenPressedAtLeast_N_Times = (m_DropCounter > N_TIMES);
			}
		}
	}
	
	public boolean getLeftHasBeenPressedAtLeast_N_Times()
	{
		return m_LeftHasBeenPressedAtLeast_N_Times;
	}
	
	public boolean getRightHasBeenPressedAtLeast_N_Times()
	{
		return m_RightHasBeenPressedAtLeast_N_Times;
	}
	
	public boolean getRotateHasBeenPressedAtLeast_N_Times()
	{
		return m_RotateHasBeenPressedAtLeast_N_Times;
	}
	
	public boolean getDropHasBeenPressedAtLeast_N_Times()
	{
		return m_DropHasBeenPressedAtLeast_N_Times;
	}
}
